package cn.b.sky.admin.controller;

import cn.b.sky.i18n.service.I18nService;
import cn.b.sky.admin.constants.SystemConstant;
import cn.b.sky.admin.enums.DwzCallbackType;
import cn.b.sky.admin.enums.DwzStatusCode;
import cn.b.sky.admin.model.BatchJobReport;
import cn.b.sky.admin.model.DWZResult;
import cn.b.sky.admin.model.Language;
import cn.b.sky.admin.model.Site;
import cn.b.sky.admin.model.condition.LanguageCondition;
import cn.b.sky.admin.service.AdminSiteService;
import cn.b.sky.admin.service.LanguagesService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/management/language/*")
public class LanguageController extends BaseController {

  @Resource
  private LanguagesService languagesService;

  @Resource
  private AdminSiteService adminSiteService;

  @Resource
  private I18nService i18nService;

  @RequestMapping("language_list")
  public String listLanguage(@ModelAttribute("condition") LanguageCondition condition, String navTabId, Model model) {
    List<Language> list = languagesService.selectLanguageByCondition(condition);
    int count = languagesService.selectLanguageCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute("navTabId", navTabId);
    return "admin/language/language_list";
  }

  @RequestMapping("language_add")
  public String addLanguage(Model model, String navTabId) {
    model.addAttribute("navTabId", navTabId);
    return "admin/language/language_add";
  }

  @RequestMapping("verifylanguage")
  @ResponseBody
  public boolean verifyLanguage(String languageCode, Model model) {
    if (StringUtils.isNotBlank(languageCode) && null != languagesService.verifyLanguage(languageCode)) {
      return false;
    }
    return true;
  }

  @RequestMapping("insertlanguage")
  @ResponseBody
  public DWZResult insertLanguage(Language language, Model model, String navTabId, HttpServletRequest request) {
    LanguageCondition condition = new LanguageCondition();
    condition.setLanguageCode(language.getLanguageCode());
    languagesService.insertLanguage(language);
    List<Site> sites = adminSiteService.seleceSite();
    for (Site site : sites) {
      i18nService.flushLocaleResourceBundleByLanguage(site.getSiteCode(), language.getLanguageCode());
    }
    return dialogAjaxDone(SystemConstant.DWZ_CODE_OK_MESSAGE, null, null, null, null);
  }

  @RequestMapping("language_modify")
  public String modifyLanguage(Model model, String navTabId, String code) {
    Language language = languagesService.selectLanguageByCode(code);
    model.addAttribute("language", language);
    model.addAttribute("navTabId", navTabId);
    return "admin/language/language_edit";
  }

  @RequestMapping("language_update")
  @ResponseBody
  public DWZResult updateLanguage(Language language, Model model, String navTabId, HttpServletRequest request) {
    languagesService.updateLanguage(language);
    return dialogAjaxDone(SystemConstant.DWZ_CODE_OK_MESSAGE, null, null, null, null);
  }

  @RequestMapping("language_delete")
  @ResponseBody
  public DWZResult deleteLanguage(String languageCodes, String navTabId, Model model, HttpServletRequest request) {
    if (StringUtils.isBlank(languageCodes)) {
      return ajaxError("请选择要删除的语言");
    }
    String[] codeArr = languageCodes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(codeArr.length);
    boolean hasFail = false;
    for (String code : codeArr) {
      BatchJobReport report = languagesService.deleteLanguage(code);
      List<Site> sites = adminSiteService.seleceSite();
      for (Site site : sites) {
        i18nService.deleteLocaleResourceBundle(site.getSiteCode(), code);
      }
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      return navTabAjaxDone(navTabId, null, null, null, SystemConstant.DWZ_CODE_OK_MESSAGE, null);
    } else {
      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("title", "详细报告");
      map.put("data", reports);
      String jsonData = JSON.toJSONString(map);
      String forwardUrl = request.getContextPath() + "/manage/common/showReport";
      return getDWZJsonObject(DwzStatusCode.OK, null, null, null, DwzCallbackType.EMPTY, forwardUrl, null, jsonData);
    }
  }
}
